package com.hyt.it.ogt.ks.controller;

import java.nio.charset.StandardCharsets;

import javax.annotation.Resource;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.utils.result.Result;
import com.hyt.it.ogt.ks.feign.KwClient;
import com.hyt.it.ogt.ks.feign.TpsClient;
import com.hyt.it.ogt.ks.feign.model.ExamAgreement;
import com.hyt.it.ogt.ks.util.Constants;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "19.考生须知", value = "考生须知")
@ApiSort(19)
@Slf4j
@RequestMapping("/ks/agreement")
@RestController
public class ExamAgreementController {
    @Resource
    private TpsClient tpsClient;
    @Resource
    private KwClient kwClient;

    @ApiOperation("19.1 获取考生须知")
    @ApiOperationSort(1)
    @GetMapping("get")
    public ResponseEntity<byte[]> get(@RequestParam String examId) {
        String repRtn = kwClient.getAgreement(examId);
        ApiResponse<ExamAgreement> clientResponse = ApiResponse.ConvertRet(repRtn, ExamAgreement.class);
        ExamAgreement examAgreement = clientResponse.getData();
        String res = tpsClient.getStringFromHuaweiObs(examAgreement.getContent(), Constants.CHARSET_UTF8);
        Result result = JSON.parseObject(res, Result.class);
        String content = result.getObj().toString();
        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
        HttpHeaders headers = new HttpHeaders();
        headers.add("X-Frame-Options", "SAMEORIGIN"); // 解决IFrame拒绝的问题);
        headers.setContentLength(bytes.length);
        headers.setContentType(MediaType.TEXT_HTML);
        return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
    }
}
